package com.ljh.myaiagent.utils;

import com.ljh.myaiagent.entity.ChatMessage;
import org.springframework.ai.chat.messages.*;

import java.util.List;

public class MessageConverter {

    /**
     * 将 Message 对象转换为 ChatMessage
     */
    public static ChatMessage toChatMessage(String conversationId, Message message) {
        return ChatMessage.builder()
                .conversationId(conversationId)
                .messageType(message.getMessageType())
                .content(message.getText())
                .metadata(message.getMetadata())
                .build();
    }

    /**
     * 将 ChatMessage 对象转换为 Message
     */
    public static Message toMessage(ChatMessage chatMessage) {
        return switch (chatMessage.getMessageType()) {
            case USER -> new UserMessage(chatMessage.getContent());
            case ASSISTANT -> new AssistantMessage(chatMessage.getContent(), chatMessage.getMetadata());
            case SYSTEM -> new SystemMessage(chatMessage.getContent());
            case TOOL -> new ToolResponseMessage(List.of(), chatMessage.getMetadata());
        };
    }
}
